/**
 * @(#)$CurrentFile
 * 版权声明 厦门亿力吉奥软件公司, 版权所有 违者必究
 *
 *<br> Copyright:  Copyright (c) 2006
 *<br> Company:厦门亿力吉奥软件公司
 *<br> @author 耿渊
 *<br> 2007-07-01
 *<br> @version 1.0
 *————————————————————————————————————
 *修改记录
 *    修改者：
 *    修改时间：
 *    修改原因：
 *——————————————————————————————————————
 */
package com.sunsharing.flow.eventman;

import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;

import com.sunsharing.common.utils.StringUtils;
import com.sunsharing.flow.event.ApplicationEvent;
import com.sunsharing.flow.event.WorkFlowEvent;
import org.apache.log4j.Logger;



/**
 *<pre>Event</pre>
 *<br>--------------------------------------------------------------------------
 *<br> <b>功能描述:</b>
 *<br>   事件管理者,负责事件的传播和事件注册
 *<br> 注意事项:
 *<br> jdk 1.4
 *<br> jsp 1.2
 *<br>--------------------------------------------------------------------------
 *<br>
 */


public class ApplicationEventMulticaster implements EventMulticaster {

    /**日志**/
    protected Logger logger = Logger.getLogger(this.getClass());

    /**监听器**/
    protected Map applicationListeners = new HashMap();

    public ApplicationEventMulticaster() {
    }

    /**
     * 功能描述:
     *    增加监听器
     * @param
     * @param
     */
    public void addApplicationListener(Object object,
                                       ApplicationListener listenerDesc) {
        //获得该事件对应的监听器列表
        List list = (List) applicationListeners.get(object);
        //如果列表为空,则初始化
        if (list == null) {
            list = new ArrayList();
            applicationListeners.put(object, list);
        }

        list.add(listenerDesc);

    }


    /**
     * 功能描述:
     *    移除监听器
     * @param
     * @param
     */
    public void removeApplicationListener(Object object,
                                          ApplicationListener listenerDesc) {
        //获得该事件对应的监听器列表
        List list = (List) applicationListeners.get(object);
        //如果列表为空,则初始化
        if (list != null) {
            //list = new ArrayList();
            //applicationListeners.put(object, list);
            list.remove(listenerDesc);
        }



    }


    /**
     * 功能描述:
     *    移除所有的监听器
     */
    public void removeAllListeners() {
        applicationListeners.clear();

    }


    /**
     * 功能描述:
     *    发布事件
     * @param event ApplicationEvent
     */
    public void multicastEvent(ApplicationEvent event) {

        //logger.debug("传播事件的类名:" + event.getClass().getName());
        //获得该事件对应的监听器列表
        List list = (List) applicationListeners.get(event.getSource());
       // System.out.println(".............."+event.getSource());
        for(Iterator iter = applicationListeners.keySet().iterator();iter.hasNext();)
        {
            Object obj = iter.next();
               //System.out.println("...."+obj);
            if(event.getSource()==obj)
            {
                //System.out.println("true......");
            }
        }
        //如果列表为空,则初始化
        if (list == null) {

            //list = new ArrayList();
            //applicationListeners.put(event.getSource(), list);
        } else {

            Iterator itor = list.iterator();
            while (itor.hasNext()) {
                ApplicationListener listenerDesc = (ApplicationListener) itor.
                        next();
                executeListener(listenerDesc, event);
            }
        }

    }


    /**
     * 功能描述:
     *    执行监听器
     * @param listenerDesc ListenerDescription
     * @param event ApplicationEvent
     */
    private void executeListener(ApplicationListener listenerDesc,
                                 ApplicationEvent event) {

        try {
            //获得是否异步
            //boolean isAsync = listenerDesc.isAsync();

            if (false) {

            } else {
                execute(listenerDesc, event);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("监听器[" + listenerDesc + "]执行失败,该监听器不能被启用,"
                         + "错误信息为:" + e.getMessage());
        }
    }

    /**
     * 功能描述:
     *    执行监听器
     * @param listenerDesc ListenerDescription
     * @param event ApplicationEvent
     */
    private void execute(ApplicationListener listenerDesc,
                         ApplicationEvent event) {
        //获得表达式
        //String expression = listenerDesc.getExpression();
        //如果表达式不为空,则执行
//        if (StringUtils.isBlank(expression)) {
//
//        } else if (listenerDesc.getListener() != null) {
//
//            listenerDesc.getListener().onApplicationEvent(event.getSource(),event);
//        }

        listenerDesc.onApplicationEvent(event.getSource(),event);
    }

    public static void main(String[] args) {

        //NodeEnterEvent event = new NodeEnterEvent();
      

    }

}
